package com.edu.web.blog;

import com.edu.entity.blog.Article;
import com.edu.entity.blog.blogComment;
import com.edu.entity.chat.Page;
import com.edu.entity.user.EduUser;
import com.edu.service.blog.ArticleService;
import com.edu.service.blog.BlogCommentService;
import com.edu.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.security.Principal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author: LuckyBear
 * @date: 2018-10-31 8:55
 * @description:
 */

@Controller
public class BolgIndexController {
    private static final int PAGE_COUNT = 4;

    @Autowired
    public ArticleService articleService;


    @Autowired
    public BlogCommentService blogCommentService;

    @Autowired
    private UserService userService;

    @RequestMapping("/listBlog")
    public String listBlog(HttpServletRequest request, Model model) {
        int currentPage = 1;
        String page = request.getParameter("page");
        if (page != null) {
            currentPage = Integer.valueOf(page);
        }

        List<Article> articles = articleService.listBlogByTime(new Page((currentPage - 1) * PAGE_COUNT, PAGE_COUNT));

        int totalPage = articleService.getTotal(PAGE_COUNT);
        model.addAttribute("articles", articles);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", "?page=" + (currentPage + 1));
        return "blog/blogIndex";
    }

    @RequestMapping("/Search")
    public String Search(Model model, String articletitle) {
        List<Article> articles = articleService.findBlogByName(articletitle);
        model.addAttribute("articles", articles);
        return "blog/list";
    }




    @RequestMapping(value = "/saveBlog")
    private String saveBlog(HttpServletRequest request, @RequestParam("file") MultipartFile myfile,Principal principal) {
        String articletitle = request.getParameter("articletitle");
        String brief = request.getParameter("brief");
        String articlecontent=request.getParameter("articlecontent");
        EduUser eduUser=userService.findUserByName(principal.getName());
        int pid=eduUser.getId();
        Date date=new Date();
        String newFileName="";
        String oldFileName = myfile.getOriginalFilename();//获取上传文件原名
        String saveFilePath = "E:/upload/";
        if (myfile != null && oldFileName != null && oldFileName.length() > 0) {
            // 新的图片名称
            newFileName = UUID.randomUUID() + oldFileName.substring(oldFileName.lastIndexOf("."));
            // 新图片
            File newFile = new File(saveFilePath + "\\" + newFileName);
            // 将内存中的数据写入磁盘
            try {
                myfile.transferTo(newFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Article article=new Article(articletitle,date,pid,articlecontent,newFileName,brief);
        articleService.createBlog(article);
        return "redirect:listBlog";
    }



    @RequestMapping("/createBlog")
    public String createBlog(Model model, Principal principal){
        int id = userService.findUserByName(principal.getName()).getId();
        List<Article> myBlog = articleService.myBlog(id);
        model.addAttribute("myBlog",myBlog);
        return "blog/createBlog";
    }

    @RequestMapping("/showBlog")
    public String showBlog(Model model, int id){
        Article article = articleService.findBlogById(id);
        List<blogComment> blogCommentList = blogCommentService.displayComment(id);
        model.addAttribute("blogCommentList",blogCommentList);
        model.addAttribute("article",article);
        return "blog/showBlog";
    }

    @RequestMapping("/writeComment")
    private String writeComment(int articleid, Principal principal, blogComment blogComment){
        int commenterid = userService.findUserByName(principal.getName()).getId();
        blogComment.setCommenttime(new Date());
        blogComment.setCommenterid(commenterid);
        blogComment.setArticleid(articleid);
        int i = blogCommentService.addComment(blogComment);
        return "redirect:showBlog?id="+articleid;
    }

}
